package com.huya.k12.translate.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huya.k12.translate.dao.WordDao;
import com.huya.k12.translate.model.entity.Word;
import com.huya.k12.translate.model.query.WordQuery;
import com.huya.k12.translate.model.vo.PageControl;
import com.huya.k12.translate.model.vo.WordVo;
import com.huya.k12.translate.service.WordService;
import com.huya.k12.translate.util.XMLUtil;

@Service
public class WordServiceImpl implements WordService{

	@Autowired
	private WordDao wordDao;
	
	@Autowired 
	private XMLUtil xmlUtil;
	
	@Override
	public Word get(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Word> listAll() {
		// TODO Auto-generated method stub
		return wordDao.listAll();
	}

	@Override
	public Integer countData(WordQuery query) {
		// TODO Auto-generated method stub
		return wordDao.countData(query);
	}

	@Override
	public List<Word> listByQuery(WordQuery query, PageControl pageControl) {
		// TODO Auto-generated method stub
		return wordDao.listByQuery(query, pageControl);
	}

	@Override
	public boolean init(List<Word> wordList) {
		// TODO Auto-generated method stub
		return wordDao.init(wordList);
	}

	@Override
	public Word get(String wordId) {
		// TODO Auto-generated method stub
		return wordDao.get(wordId);
	}

	@Override
	public boolean edit(Word word) {
		// TODO Auto-generated method stub
		return wordDao.update(word);
	}

	@Override
	public List<Word> listByStatus(Integer status) {
		// TODO Auto-generated method stub
		return wordDao.listByStatus(status);
	}

	@Override
	public boolean addWord(List<Word> list) {
		// TODO Auto-generated method stub
		return wordDao.addWord(list);
	}

	@Override
	public boolean updateWord(List<Word> list) {
		// TODO Auto-generated method stub
		return wordDao.updateWord(list);
	}

	@Override
	public List<WordVo> wordVoList() {
		// TODO Auto-generated method stub
		List<Word> wordList = listAll();
		if(wordList!=null && wordList.size() > 0){
			List<WordVo> wordVoList = new ArrayList<WordVo>();
			for(Word word : wordList){
				WordVo wordVo = wordToWordVo(word);
				wordVoList.add(wordVo);
			}
			return wordVoList;
		}
		return null;
	}
	
	
	private WordVo wordToWordVo(Word word){
		if(word != null){
			WordVo wordVo = new WordVo();
			Map<String, String> map = new HashMap<String, String>();
			wordVo.setWordId(word.getWordId());
			wordVo.setFileName(word.getFileName());
			map.put("en", word.getEn());
			map.put("ar", word.getAr());
			map.put("de", word.getDe());
			map.put("es", word.getEs());
			map.put("fr", word.getFr());
			map.put("hi", word.getHi());
			map.put("in", word.getIndia());
			map.put("it", word.getIt());
			map.put("ja", word.getJa());
			map.put("ko", word.getKo());
			map.put("ms", word.getMs());
			map.put("pt", word.getPt());
			map.put("ru", word.getRu());
			map.put("th", word.getTh());
			map.put("tl", word.getTl());
			map.put("tr", word.getTr());
			map.put("ur-rPK", word.getUr_rPK());
			map.put("vi", word.getVi());
			map.put("zh", word.getZh());
			map.put("zh-rCN", word.getZh_rCN());
			map.put("zh-rSG", word.getZh_rSG());
			wordVo.setLanguage(map);
			return wordVo;
		}
		return null;
	}

	@Override
	public void updateByCountryCode(Map<String, String> map, String countryCode) {
		// TODO Auto-generated method stub
		wordDao.updateByCountryCode(map, countryCode);
	}

	@Override
	public void initDB() {
		wordDao.deleteAll();
		
		xmlUtil.loadDefaultXml();
		xmlUtil.loadOtherXml();
		List<Word> list = xmlUtil.getWordList();
		if(list!=null && list.size() > 0){
			wordDao.init(list);
		}
		
	}

	@Override
	public void resetStatus() {
		// TODO Auto-generated method stub
		wordDao.resetStatus();
	}

	@Override
	public void deleteWord(List<Word> list) {
		// TODO Auto-generated method stub
		wordDao.deleteWord(list);
	}
	

}
